package com.menu.system.service.impl;

import com.menu.common.core.domain.AjaxResult;
import com.menu.common.utils.DateUtils;
import com.menu.common.utils.SecurityUtils;
import com.menu.common.utils.StringUtils;
import com.menu.system.domain.MenuProductInfo;
import com.menu.system.domain.MenuProducts;
import com.menu.system.domain.vo.ProductHeartbeatVo;
import com.menu.system.mapper.MenuProductInfoMapper;
import com.menu.system.mapper.MenuProductsMapper;
import com.menu.system.mapper.MenuProtypeMapper;
import com.menu.system.mapper.SysUserRoleMapper;
import com.menu.system.service.IMenuProductsService;
import com.menu.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 角色信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-05
 */
@Service
public class MenuProductsServiceImpl implements IMenuProductsService {
    @Autowired
    private MenuProductsMapper menuProductsMapper;
    @Autowired
    private MenuProductInfoMapper menuProductInfoMapper;
    @Autowired
    private MenuProtypeMapper menuProtypeMapper;
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private SysUserRoleMapper userRoleMapper;
    /**
     * 查询角色信息
     *
     * @param productId 角色信息主键
     * @return 角色信息
     */
    @Override
    public MenuProducts selectMenuProductsByProductId(Long productId) {


        return menuProductsMapper.selectMenuProductsByProductId(productId);
    }

    /**
     * 查询角色信息列表
     *
     * @param menuProducts 角色信息
     * @return 角色信息
     */
    @Override
    public List<MenuProducts> selectMenuProductsList(MenuProducts menuProducts) {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            return menuProductsMapper.selectMenuProductsList(menuProducts);
        }else{
            menuProducts.getParams().put("roleIds", userRoleMapper.UserRoles(SecurityUtils.getUserId()).stream().toArray(Long[]::new));
            List<Long> longs = userRoleMapper.UserRoles(SecurityUtils.getUserId());

            System.out.println(longs);
            return menuProductsMapper.selectMenuProductsListRole(menuProducts);
        }

    }

    /**
     * 新增角色信息
     *
     * @param menuProducts 角色信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMenuProducts(MenuProducts menuProducts, String inserType) {
        if (MenuProducts.inserType.INSERT.getInfo().equals(inserType)) {
            /*增加*/
            menuProducts.setCreateTime(DateUtils.getNowDate());
            menuProducts.setCreateBy(SecurityUtils.getUsername());

            int i = menuProductsMapper.insertMenuProducts(menuProducts);
            batchMenuProductInfoRoles(menuProducts.getRoles(), menuProducts.getProductId());
            batchMenuProductRoles(menuProducts);
            batchMenuProductInfoPost(menuProducts);
            batchMenuHeartbeatproducts(menuProducts);
            return i;
        } else {
//            menuProducts.setProdTypes(menuProtypeMapper.selectMenuProtypeids(Arrays.asList(menuProducts.getProductModel().split(","))));

            /*导入*/
            menuProducts.setCreateTime(DateUtils.getNowDate());
            menuProducts.setCreateBy(SecurityUtils.getUsername());
            int i = menuProductsMapper.insertMenuProducts(menuProducts);
            batchMenuProductInfoPost(menuProducts);
            return i;
        }

    }

    /**
     * 修改角色信息
     *
     * @param menuProducts 角色信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMenuProducts(MenuProducts menuProducts) {
        /*删除产品*/
        menuProductsMapper.deleteMenuProductsByProductId(menuProducts.getProductId());
        /*删除产品类型关联*/
        menuProductInfoMapper.deleteMenuProductInfoByProductId(menuProducts.getProductId());
        /*删除产品角色关联*/
        menuProductInfoMapper.deleteMenuPmenuproductRole(menuProducts.getProductId());
        /*删除产品推荐*/
        menuProductsMapper.deletemenuproductHeartbeatproducts(menuProducts.getProductId());
        Long productId = menuProducts.getProductId();
        menuProducts.setCreateTime(DateUtils.getNowDate());
        menuProducts.setCreateBy(SecurityUtils.getUsername());
        menuProducts.setUpdateBy(SecurityUtils.getUsername());
        int i = menuProductsMapper.insertMenuProducts(menuProducts);
        /*删除角色与产品关联*/
//        menuProductsMapper.updateMenuProductRoles(productId,menuProducts.getProductId());
        batchMenuProductInfoRoles(menuProducts.getRoles(), menuProducts.getProductId());
//        batchMenuProductRoles(menuProducts);
        batchMenuProductInfoPost(menuProducts);
        batchMenuHeartbeatproducts(menuProducts);
       return  i;
    }



    /**
     * 批量删除角色信息
     *
     * @param productIds 需要删除的角色信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMenuProductsByProductIds(Long[] productIds) {
        int i = 0;
        for (Long productId : productIds) {

            i= menuProductsMapper.deleteMenuProductsByProductId(productId);
            /*删除产品类型关联*/
            menuProductInfoMapper.deleteMenuProductInfoByProductId(productId);
            /*删除产品角色关联*/
            menuProductInfoMapper.deleteMenuPmenuproductRole(productId);
            /*删除产品推荐*/
            menuProductsMapper.deletemenuproductHeartbeatproducts(productId);
            /*删除角色与产品关联*/
            menuProductsMapper.deleteMenuProductRoles(productId);
        }

        return i;
    }

    /**
     * 删除角色信息信息
     *
     * @param productId 角色信息主键
     * @return 结果
     */


    @Override
    public List<MenuProducts> selectmenuProducts(MenuProducts menuProducts) {
        return menuProductsMapper.selectMenuProductsLists(menuProducts);
    }

    @Override
    public List<MenuProducts> selectMenuProductsListExecl(MenuProducts menuProducts) {
        return menuProductsMapper.selectMenuProductsListExecl(menuProducts);
    }

/*产品推荐查询*/
    @Override
    public List<Long> findMenuHeartbeatproducts(Long productId) {
        return menuProductsMapper.findMenuHeartbeatproducts(productId);
    }

    @Override
    public List<String> queryUserDefinedConditions() {
        return menuProductsMapper.queryUserDefinedConditions();
    }

    @Override
    public List<Long> queryUserProduct(Long productId) {
        return menuProductsMapper.queryUserProduct(productId);
    }

    @Override
    @Transactional
    public AjaxResult editProductRole(MenuProducts menuProducts) {
        menuProductsMapper.deleteMenuProductRoles(menuProducts.getProductId());

        for (Long role : menuProducts.getRoles()) {
            menuProductsMapper.batchMenuProductRoles(menuProducts.getProductId(),role);

        }


        return AjaxResult.success();
    }

    /*产品关联类型*/
    public void batchMenuProductInfoPost(MenuProducts menuProducts) {
        Long[] posts = menuProducts.getProdTypes();
        if (StringUtils.isNotEmpty(posts)) {

            List<MenuProductInfo> list = new ArrayList<MenuProductInfo>(posts.length);
            for (Long postId : posts) {
                MenuProductInfo up = new MenuProductInfo();
                up.setProductId(menuProducts.getProductId());
                up.setProductypeId(postId);
                list.add(up);
            }
            menuProductInfoMapper.batchMenuProductInfoPost(list);
        }


    }
/*产品关联角色*/
    public void batchMenuProductInfoRoles(Long [] roleId, Long productId) {
        List<MenuProductInfo> userPostList=new ArrayList<>();

        for (Long l : roleId) {
            MenuProductInfo list=new MenuProductInfo();
            list.getParams().put("roleId",l);
            list.getParams().put("productId",productId);
            userPostList.add(list);
        }
        menuProductInfoMapper.batchMenuProductInfoRoles(userPostList);

    }
/*产品推荐增加*/
    public void batchMenuHeartbeatproducts(MenuProducts menuProducts) {
        List<ProductHeartbeatVo> userPostList=new ArrayList<>();
        if (menuProducts.getHeartbeatProduct()!=null&&menuProducts.getHeartbeatProduct().length>0){
            for (Long l : menuProducts.getHeartbeatProduct()) {
                ProductHeartbeatVo list=new ProductHeartbeatVo();
                list.setProductId(menuProducts.getProductId());
                list.setHeartbeatProductId(l);
                userPostList.add(list);
            }
            menuProductsMapper.batchMenuHeartbeatproducts(userPostList);
        }


    }
    public void batchMenuProductRoles(MenuProducts menuProducts){
        Long[] array = userRoleMapper.UserRoles(SecurityUtils.getUserId()).stream().toArray(Long[]::new);
        for (Long roleId :   array) {
            menuProductsMapper.batchMenuProductRoles(menuProducts.getProductId(),roleId);
        }


    }
}
